package com.zigong.app.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.jiongbull.jlog.JLog;
import com.zigong.app.R;
import com.zigong.app.base.BaseActivity;
import com.zigong.app.ui.utils.GlideUtil;
import com.zigong.app.ui.utils.SampleListener;
import com.zigong.app.ui.utils.TitleUtils;
import com.zigong.app.ui.widget.SimplePlayerView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 热门视频
 */
public class VideoPlayActivity extends BaseActivity {

    public static final String EXTRA_POS = "ex_pos";
    public static final String EXTRA_POS_ITEM = "ex_pos_item";
    public static final String EXTRA_COVER = "ex_cover";

    @BindView(R.id.video_play_player)
    protected SimplePlayerView simplePlayerView;

    private String mUrl;//视频的url

    private int itemPos;
    private int playPos;
    private String coverImgUrl;//封面的url

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this);
        Intent intent = getIntent();
        if (intent == null) {
            toast(R.string.data_wrong);
            finish();
            return;
        }
        mUrl = intent.getStringExtra(EX_URL);
        playPos = intent.getIntExtra(EXTRA_POS, -1);
        itemPos = intent.getIntExtra(EXTRA_POS_ITEM, -1);
        coverImgUrl= intent.getStringExtra(EXTRA_COVER);

        JLog.e("bbab  get play pos="+playPos+"  item pos="+itemPos+"\n "+mUrl);
        if (TextUtils.isEmpty(mUrl)) {
            toast(R.string.data_wrong);
            finish();
            return;
        }

        simplePlayerView.setStandardVideoAllCallBack(new SampleListener() {
            @Override
            public void onClickStartThumb(String url, Object... objects) {
                super.onClickStartThumb(url, objects);
                JLog.e("iiai  url=" + url);
            }

            @Override
            public void onClickBlank(String url, Object... objects) {
                super.onClickBlank(url, objects);
//                JLog.e("iiai  url="+url);
                setPlayResult(false);
                finish();
            }

            @Override
            public void onAutoComplete(String url, Object... objects) {
                super.onAutoComplete(url, objects);
//                simplePlayerView.startPlayLogic();
                setPlayResult(true);
                finish();
            }
        });
        simplePlayerView.setUp(mUrl, true, "");
        if (playPos > 0) {
            simplePlayerView.setPlayPosition(playPos);
        }
        simplePlayerView.startPlayLogic();

        if (simplePlayerView != null && simplePlayerView.getThumbImge() != null && !TextUtils.isEmpty(coverImgUrl)) {
            GlideUtil.getGlideWithBlackBg(this, coverImgUrl).into(simplePlayerView.getThumbImge());
        }
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_video_play);
        ButterKnife.bind(this);
    }

    @Override
    public void onBackPressed() {
        setPlayResult(false);
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        simplePlayerView.onVideoResume();
    }

    @Override
    protected void onPause() {
        simplePlayerView.onVideoPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        simplePlayerView.release();
        super.onDestroy();
    }

    private void setPlayResult(boolean isCompelet) {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_POS_ITEM, itemPos);
        if (isCompelet) {
            setResult(RESULT_OK, intent);
        } else {
            int pos=simplePlayerView.getCurrentPositionWhenPlaying();
            JLog.e("bbab  will set pos "+pos);
            intent.putExtra(EXTRA_POS, pos);
            setResult(RESULT_CANCELED, intent);
        }
    }
}
